function* range(start: number, end: number) {
    for (let i = start; i <= end; i++)
        yield i;
}
for (let i of range(1, 10)) {
    console.log(i);
}

// 练习【黑白棋】：编写生成器，从一个落子的位置开始，沿某个方向的所有棋子（可能有八个方向）
type ChessType = 'black' | 'white'
type ChessState = ChessType | '_____'
type Point = {
    readonly x: number
    readonly y: number
}
type CheckerBoard = {
    readonly max_x: number
    readonly max_y: number
    readonly chesses: ChessState[][]
}

// 判断 p1 和 p2 是否处于同一行/列/对角
const checkPos = (p1: Point, p2: Point): boolean => {
    return (p1.x === p2.x) || (p1.y === p2.y) || (p1.x - p1.y === p2.x - p2.y) || (p1.x + p1.y === p2.x + p2.y)
}

// TODO: 函数式编程重构之
// 从一个位置（from）开始，沿着八个方向能够覆盖到的所有棋子
const getAllCovered = function* (board: CheckerBoard, from: Point, chess: ChessType) {
    for (let i = 0; i < board.max_x; i++) {
        for (let j = 0; j < board.max_y; j++) {
            if (board.chesses[i][j] === chess && checkPos(from, { x: i, y: j })) {
                yield [i, j]
            }
        }
    }
}

const board: CheckerBoard = {
    max_x: 4,
    max_y: 4,
    chesses: [
        ['black', 'white', 'black', 'black'],
        ['black', 'white', 'black', 'white'],
        ['black', 'black', 'black', 'black'],
        ['black', 'black', 'white', 'black'],
    ]
}

const from: Point = {
    x: 1,
    y: 1,
}

const chess: ChessType = 'black'

let resultChesses: ChessState[][] = [
    ['_____', '_____', '_____', '_____'],
    ['_____', '_____', '_____', '_____'],
    ['_____', '_____', '_____', '_____'],
    ['_____', '_____', '_____', '_____'],
]

for (let [x, y] of getAllCovered(board, from, chess)) {
    resultChesses[x][y] = 'black'
}

console.log(resultChesses)